#include "clientSocket.h"

ClientSocket::ClientSocket(char *ip, unsigned int puerto) {
	this->socketFd = socket(AF_INET, SOCK_STREAM, 0);
	this->puerto = puerto;
	this->ip = ip;
}

int ClientSocket::getSocketFd() const {
    return socketFd;
}

int ClientSocket::conectar() {
	struct sockaddr_in serverAddress;
	struct hostent * hostIp = gethostbyname(this->ip);
	int resultado;

	serverAddress.sin_family = AF_INET;
	serverAddress.sin_port = htons(puerto);
	serverAddress.sin_addr = *((struct in_addr *)hostIp->h_addr);
	resultado = connect(socketFd,
						(struct sockaddr *)&serverAddress,
						sizeof(serverAddress));
	return resultado;
}


void ClientSocket::cerrar() {
	shutdown(this->socketFd, SHUT_RDWR);
	close(this->socketFd);
}
